/**
 * @file example_005.cpp
 * @author 逆流 (1171267147@qq.com)
 * @brief 虚表内存布局测试
 * @version 1.0
 * @date 2025-07-07
 *
 * @copyright Copyright (c) 2025
 *
 */
#include <iostream>

using std::cout;

class Base {
public:
    Base() { cout << this << "\t" << &a_ << "\n"; }
    virtual ~Base() = default;

    void         func_1() {};
    virtual void func_2() { cout << "Base" << "\n"; }

private:
    int a_{0};
};

class Drived : public Base {
public:
    Drived() { cout << this << "\t" << &b_ << "\n"; }
    ~Drived() = default;

    void func_1() {}
    void func_2() override { cout << "Drived" << "\n"; }

private:
    int b_{0};
};

int main() {
    Base* base = new Base();
    cout << base << "\n";

    base->func_1();
    base->func_2();

    Drived* drived = new Drived();
    cout << drived << "\n";

    Base& base_drived = *drived;
    base_drived.func_2();

    return 0;
}

/*
reakpoint 1, Base::func_2 (this=0x55555556b2b0) at /home/yangjian/Workspace/Project/cpp_hodgepodge/idiom/example_005.cpp:11
11          virtual void func_2() { cout << "Base" << "\n"; }
(gdb) p this
$10 = (Base *) 0x55555556b2b0
(gdb) p *this
$11 = {_vptr$Base = 0x555555557d40 <vtable for Base+16>, a_ = 0}
(gdb) p 0x555555557d40
$12 = 93824992247104
(gdb) x/8x 0x555555557d40
0x555555557d40 <_ZTV4Base+16>:  0x555553c0      0x00005555      0x555553d0      0x00005555
0x555555557d50 <_ZTV4Base+32>:  0x55555400      0x00005555      0xf7e6dd28      0x00007fff
(gdb) p *(Quit
(gdb) p *(0x5555555553c0)
$13 = -443987883
(gdb) x/x 0x5555555553c0
0x5555555553c0 <_ZN4BaseD2Ev>:  0xe5894855
(gdb) x/x 0x5555555553d0
0x5555555553d0 <_ZN4BaseD0Ev>:  0xe5894855
(gdb) x/x 0x555555555400
0x555555555400 <_ZN4Base6func_2Ev>:     0xe5894855
(gdb) c
Continuing.
Base
0x55555556b6e0  0x55555556b6e8
0x55555556b6e0  0x55555556b6ec
0x55555556b6e0

Breakpoint 2, Drived::func_2 (this=0x55555556b6e0) at /home/yangjian/Workspace/Project/cpp_hodgepodge/idiom/example_005.cpp:23
23          void func_2() override { cout << "Drived" << "\n"; }
(gdb) p this
$14 = (Drived *) 0x55555556b6e0
(gdb) p *this
$15 = {<Base> = {_vptr$Base = 0x555555557d78 <vtable for Drived+16>, a_ = 0}, b_ = 0}
(gdb) x/8x 0x555555557d78
0x555555557d78 <_ZTV6Drived+16>:        0x55555440      0x00005555      0x55555460      0x00005555
0x555555557d88 <_ZTV6Drived+32>:        0x55555490      0x00005555      0xf7e6ebe8      0x00007fff
(gdb) x/x 0x555555555440
0x555555555440 <_ZN6DrivedD2Ev>:        0xe5894855
(gdb) x/x 0x555555555460
0x555555555460 <_ZN6DrivedD0Ev>:        0xe5894855
(gdb) x/x 0x555555555490
0x555555555490 <_ZN6Drived6func_2Ev>:   0xe5894855
(gdb) 
*/
